package com.dao;
/***
*
*/
import com.util.*;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class ShopDao {
	public ShopDao(){
	
	}
	public int addShop(HttpServletRequest req){
		
		String userid = Util.getSeq();
		String shopname=req.getParameter("shopname")==null?"":req.getParameter("shopname");
		String shopphone=req.getParameter("shopphone")==null?"":req.getParameter("shopphone");
		String shopower=req.getParameter("shopower")==null?"":req.getParameter("shopower");
		String shopaddress=req.getParameter("shopaddress")==null?"":req.getParameter("shopaddress");
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		String shopscore=req.getParameter("shopscore")==null?"":req.getParameter("shopscore");
		String sql ="insert into TBL_SHOP("+
        		"SHOPNAME"+
        		",SHOPPHONE"+
        		",SHOPOWER"+
        		",SHOPADDRESS"+
        		",CREATETIME"+
        		",SHOPSCORE"+
        		",userid"+
        		",SHOPID"+
        	") values("+
        		"'"+shopname+"'"+
        	        		",'"+shopphone+"'"+
        	        		",'"+shopower+"'"+
        	        		",'"+shopaddress+"'"+
        	        		",'"+createtime+"'"+
        	        		",'0'"+
        	        		",'"+userid+"'"+
        	        		",'"+userid+"'"+
        	")";
		
		String sql2 ="insert into USERS("+
        		"USERNAME"+
        		",PASSWORD"+
        		",USERTYPE"+
        		",userid"+

        	") values("+
        		"'"+shopname+"'"+
        	        		",'1'"+
        	        		",'1'"+
        	        		",'"+userid+"'"+
        	")";
		String [] str = new String []{sql,sql2};
        	DB db = new DB();
        	return db.batchSql(str);	
	}
	public int delShop(HttpServletRequest req){
         String shopid=req.getParameter("shopid");
		String sql ="delete from TBL_SHOP where 1=1 "+
    		" and shopid ='"+shopid+"'"+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updShop(HttpServletRequest req){
		String shopname=req.getParameter("shopname")==null?"":req.getParameter("shopname");
		String shopphone=req.getParameter("shopphone")==null?"":req.getParameter("shopphone");
		String shopower=req.getParameter("shopower")==null?"":req.getParameter("shopower");
		String shopaddress=req.getParameter("shopaddress")==null?"":req.getParameter("shopaddress");
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		String shopscore=req.getParameter("shopscore")==null?"":req.getParameter("shopscore");
        String shopid=req.getParameter("shopid");
		String sql ="update TBL_SHOP set "+
        		"SHOPNAME='"+shopname+"'"+
        		",SHOPPHONE='"+shopphone+"'"+
        		",SHOPOWER='"+shopower+"'"+
        		",SHOPADDRESS='"+shopaddress+"'"+
        		",CREATETIME='"+createtime+"'"+
        		",SHOPSCORE='"+shopscore+"'"+
        	" where 1=1 "+
    		" and shopid ='"+shopid+"'"+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findShopById(HttpServletRequest req){
         String shopid=req.getParameter("shopid");
		String sql ="select * from TBL_SHOP where 1=1 "+
    		" and shopid ='"+shopid+"'"+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	public Map<String,Object> findShopUserId(HttpServletRequest req){
        Map user =  (Map)req.getSession().getAttribute("user");
        String userid = user.get("userid").toString();
		String sql ="select * from TBL_SHOP s ,users u where s.userid=u.userid and u.userid= '"+
				userid+"'";
   	DB db = new DB();
   	Map<String,Object> map = null;
   	List<Map<String,Object>> list = db.query(sql);
   	if(list!=null&&list.size()>0){
   		map = list.get(0);
   	}
   	return map;	
	}
	public PageModel findShopByCondition(HttpServletRequest req){
		String shopname=req.getParameter("shopname")==null?"":req.getParameter("shopname");
		req.setAttribute("shopname", shopname);
		String shopphone=req.getParameter("shopphone")==null?"":req.getParameter("shopphone");
		req.setAttribute("shopphone", shopphone);
		String shopower=req.getParameter("shopower")==null?"":req.getParameter("shopower");
		req.setAttribute("shopower", shopower);
		String shopaddress=req.getParameter("shopaddress")==null?"":req.getParameter("shopaddress");
		req.setAttribute("shopaddress", shopaddress);
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		req.setAttribute("createtime", createtime);
		String shopscore=req.getParameter("shopscore")==null?"":req.getParameter("shopscore");
		req.setAttribute("shopscore", shopscore);
         String shopid=req.getParameter("shopid");
		String sql ="";
		String where="";
		if(shopname!=null&&!"".equals(shopname)){
    		where+=" and SHOPNAME like '%"+shopname+"%'";
		}
		if(shopphone!=null&&!"".equals(shopphone)){
    		where+=" and SHOPPHONE like '%"+shopphone+"%'";
		}
		if(shopower!=null&&!"".equals(shopower)){
    		where+=" and SHOPOWER like '%"+shopower+"%'";
		}
		if(shopaddress!=null&&!"".equals(shopaddress)){
    		where+=" and SHOPADDRESS like '%"+shopaddress+"%'";
		}
		if(createtime!=null&&!"".equals(createtime)){
    		where+=" and CREATETIME like '%"+createtime+"%'";
		}
		if(shopscore!=null&&!"".equals(shopscore)){
    		where+=" and SHOPSCORE like '%"+shopscore+"%'";
		}
    	String countsql ="select count(*) from TBL_SHOP where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select  * from TBL_SHOP where 1=1 "+where+" order by  shopid  desc limit "+(currentPage-1)+","+pageCount+"";
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}